package com.health.tools;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.UIData;

/**
 * @author: xiaoxin
 * date: 2018/9/10
 * describe:
 * 修改内容:
 */
public class UpdateAppHelper {

    public static void downloadApk(Context context, String downloadUrl, String msg) {

            AllenVersionChecker
                    .getInstance()
                    .downloadOnly(
                            UIData.create().setDownloadUrl(downloadUrl)
                                    .setTitle("发现新版本!")
                                    .setContent(msg)
                    )
                    .setForceRedownload(true)
//                    .setCustomVersionDialogListener(createCustomDialogOne())
                    .executeMission(context);
    }

    /**
     * 务必用库传回来的context 实例化你的dialog
     * 自定义的dialog UI参数展示，使用versionBundle
     * @return
     */
//    private static CustomVersionDialogListener createCustomDialogOne() {
//        return (context, versionBundle) -> {
//            BaseDialog baseDialog = new BaseDialog(context, R.style.BaseDialog, R.layout.layout_app_version);
//            TextView tvVersion = baseDialog.findViewById(R.id.tv_version);
//            TextView textView = baseDialog.findViewById(R.id.tv_msg);
//            try {
//                String content = versionBundle.getContent();
//                String[] split = content.split(";");
//                tvVersion.setText(split[0]);
//                textView.setText(split[1]);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return baseDialog;
//        };
//    }

    /**
     * packageName
     *
     * @param context 上下文
     * @return
     */
    public static String getPackName(Context context) {
        return context.getPackageName();
    }

    /**
     * 获取本地软件版本号
     */
    public static int getVersionCode(Context ctx) {
        int versionCode = 0;
        try {
            PackageInfo packageInfo = ctx.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(ctx.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取本地软件版本号
     */
    public static String getVersionName(Context ctx) {
        String versionName = "";
        try {
            PackageInfo packageInfo = ctx.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(ctx.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

}
